image: "node:lts-alpine"

stages:
  - build
  - test
  - deploy

# These folders are cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    # Default cache directory from https://classic.yarnpkg.com/en/docs/install-ci/#gitlab.
    - node_modules/
    # Enables git-lab CI caching. Both .cache and public must be cached, otherwise builds will fail.
    - .cache/
    - public/

yarn:install:
  stage: build
  script:
    # Install via yarn with frozen-lockfile to allow reproducible dependencies and check-files option to check if the node_modules cache is valid.
    - yarn install --frozen-lockfile --check-files --non-interactive
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_MERGE_REQUEST_ID

test:unit:
  stage: test
  needs: ["yarn:install"]
  script:
    - yarn test:unit
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_MERGE_REQUEST_ID

test:gatsby:
  stage: test
  needs: ["yarn:install"]
  script:
    - ./node_modules/.bin/gatsby info
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_MERGE_REQUEST_ID

pages:
  stage: deploy
  needs: 
    - yarn:install
    - test:unit
    - test:gatsby
  script:
    - ./node_modules/.bin/gatsby build --prefix-paths
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
